package com.jinglin.web.controller;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jinglin.controller.ABaseController;
import com.jinglin.entity.dto.TokenUserInfoDto;
import com.jinglin.entity.enums.UserActionTypeEnum;
import com.jinglin.entity.enums.VideoOrderTypeEnum;
import com.jinglin.entity.po.UserFocus;
import com.jinglin.entity.po.UserInfo;
import com.jinglin.entity.po.VideoInfo;
import com.jinglin.entity.query.UserActionQuery;
import com.jinglin.entity.vo.PaginationResultVO;
import com.jinglin.entity.vo.ResponseVO;
import com.jinglin.entity.vo.UserInfoVO;
import com.jinglin.mapper.UserFocusMapper;
import com.jinglin.service.UserActionService;
import com.jinglin.service.UserFocusService;
import com.jinglin.service.UserInfoService;
import com.jinglin.service.VideoInfoService;
import com.jinglin.web.annotation.GlobalInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/uhome")
public class UHomeController extends ABaseController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserFocusService userFocusService;
    @Autowired
    private UserFocusMapper userFocusMapper;
    @Autowired
    private VideoInfoService videoInfoService;
    @Autowired
    private UserActionService userActionService;
    @PostMapping("/getUserInfo")
    public ResponseVO getUserInfo(@NotEmpty String userId){
        UserInfoVO info = userInfoService.getUserInfoDetail(userId);
        return getSuccessResponseVO(info);
    }
    @PostMapping("/updateUserInfo")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO updateUserInfo(UserInfo userInfo){
        userInfoService.saveOrUpdate(userInfo);
        return getSuccessResponseVO(null);
    }
    @PostMapping("/focus")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO focus(@NotEmpty String focusUserId){
        UserFocus userFocus = new UserFocus();
        userFocus.setFocusUserId(focusUserId);
        userFocus.setUserId(getTokenUserInfoDto().getUserId());
        userFocus.setFocusTime(LocalDateTime.now());
        userFocusService.save(userFocus);
        return getSuccessResponseVO(null);
    }
    @PostMapping("/cancelFocus")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO cancelFocus(@NotEmpty String focusUserId){
        LambdaUpdateWrapper<UserFocus> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UserFocus::getUserId,getTokenUserInfoDto().getUserId());
        wrapper.eq(UserFocus::getFocusUserId,focusUserId);
        userFocusService.remove(wrapper);
        return getSuccessResponseVO(null);
    }

    @PostMapping("/saveTheme")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO saveTheme(@NotEmpty Integer theme){
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(getTokenUserInfoDto().getUserId());
        userInfo.setTheme(theme);
        userInfoService.updateById(userInfo);
        return getSuccessResponseVO(null);
    }
    @PostMapping("/loadFocusList")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO loadFocusList(Integer pageNo,Integer pageSize){
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        String userId = tokenUserInfoDto.getUserId();
        if (pageNo == null){
            pageNo = 1;
        }
        if (pageSize == null){
            pageSize = 15;
        }
        PaginationResultVO focusList = userFocusService.getFocusList(userId, pageNo, pageSize);
        return getSuccessResponseVO(focusList);
    }

    @PostMapping("/loadFansList")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO loadFansList(Integer pageNo,Integer pageSize){
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        String userId = tokenUserInfoDto.getUserId();
        if (pageNo == null){
            pageNo = 1;
        }
        if (pageSize == null){
            pageSize = 15;
        }
        PaginationResultVO fansList = userFocusService.getFansList(userId, pageNo, pageSize);
        return getSuccessResponseVO(fansList);
    }
    @PostMapping("/loadVideoList")
    public ResponseVO loadVideoList(Integer type,Integer orderType,Integer pageNo,String userId,String videoName){
        Integer pageSize = null;
        if (type!=null){
            // 不为null，就是查主页的视频(主页视频，我们只需要查十个)
            pageSize = 10;
        }
        if (VideoOrderTypeEnum.getByType(orderType) == null){
            orderType = VideoOrderTypeEnum.CREATE_TIME.getType();
        }
        PaginationResultVO<VideoInfo> resultVO = videoInfoService.loadVideoList(pageNo, pageSize, orderType, videoName, userId);
        return getSuccessResponseVO(resultVO);
    }

    @RequestMapping("/loadUserCollection")
    public ResponseVO loadUserCollection(@NotEmpty String userId, Integer pageNo) {
        UserActionQuery actionQuery = new UserActionQuery();
        actionQuery.setActionType(UserActionTypeEnum.VIDEO_COLLECT.getType());
        actionQuery.setUserId(userId);
        actionQuery.setPageNo(pageNo);
        actionQuery.setQueryVideoInfo(true);
        actionQuery.setOrderBy("action_time desc");
        PaginationResultVO resultVO = userActionService.getListByPage(actionQuery);
        return getSuccessResponseVO(resultVO);
    }
}
